/*
 * 文件名：ContractExtController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月13日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.ContractDropDomain;
import top.infopub.mgr.bpa.domain.ContractEntryDomain;
import top.infopub.mgr.bpa.domain.EntryDropDomain;
import top.infopub.mgr.bpa.jsonVo.ContractEntryJsonVo;
import top.infopub.mgr.bpa.jsonVo.ContractNegJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.ContractEntryService;
import top.infopub.mgr.bpa.service.ContractNegService;
import top.infopub.mgr.bpa.vo.ContractEntryVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.bpa.ContractEntry;
import top.infopub.model.bpa.ContractExt;
import top.infopub.model.bpa.CotBackup;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**对外合同
 * @author wuzhiyang
 * @version 2020年2月13日
 * @see ContractEntryController
 * @since商务管理/对外合同/合同清单
 */
@Controller
@RequestMapping("/contractEntry")
public class ContractEntryController extends GenericFlowController {


    @Autowired
    private ContractEntryService contractEntryService;
    @Autowired
    private ContractNegService contractNegService;
    @Autowired
    private ProProcessManageService proProcessManageService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;


    /**
     * 合同清单列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<ContractEntryDomain> queryDataByCondition(ContractEntryVo contractEntryVo,
                                                           ParamPage paramPage){
        Page<ContractEntryDomain> page = new Page<ContractEntryDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = contractEntryService.selectByDomainWhere(page, contractEntryVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id",id);
        ContractEntry contractEntry = contractEntryService.selectBySguid(id);
        request.setAttribute("isAuditFlag", "1");
        if (contractEntry!=null){
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractEntry.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(contractEntry.getProcessId()) && contractEntry.getProcessId().length() > 10){
                request.setAttribute("processId", contractEntry.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(contractEntry.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), contractEntry.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "bpa/lay-ContractList-detail";
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //审批
        bpaProcessManageService.auditProject(id, processId, status, auditMsg);
        return RspVo.getSuccessResponse();
    }
    /**
    *@Description: 下拉数据
    *@Param:
    *@return:
    *@Author: Tszzzzzzz
    *@date: 2021/6/21
    */
    @RequestMapping("/queryDrop")
    @ResponseBody
    public RspVo queryDrop(String proCode){
        EntryDropDomain entryDropDomain = new EntryDropDomain();
        if (StringUtils.isEmpty(proCode)){ //项目下拉
            List<BpaProjectDomain> bpaProjectDomain = contractNegService.queryByProCode();
            entryDropDomain.setBpaProjectDomain(bpaProjectDomain);
        }else{//查询合同
            List<ContractDropDomain> dropDomain = contractEntryService.selectReview(proCode);  //谈判合同
            List<ContractDropDomain> dropDomain1 =  contractEntryService.selectExt(proCode);
            dropDomain.addAll(dropDomain1);
            entryDropDomain.setDropDomain(dropDomain);
        }
        return RspVo.getSuccessResponse(entryDropDomain);
    }

    /**
    *@Description: 清单详情
    *@Param:
    *@return:
    *@Author: Tszzzzzzz
    *@date: 2021/6/21
    */
    @RequestMapping("/queryEntryDetail")
    @ResponseBody
    public RspVo  queryEntryDetail(String id){
        return contractEntryService.queryEntryDetail(id);
    }

    /**
    *@Description: 清单保存
    *@Param:
    *@return:
    *@Author: Tszzzzzzz
    *@date: 2021/6/21
    */
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr,Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        ContractEntryJsonVo vo = JSON.parseObject(s,ContractEntryJsonVo.class);
        ContractEntry contractEntry = new ContractEntry();
        if (StringUtils.isNotBlank(contractEntry.getId())){
            //编辑
            BeanUtils.copyProperties(vo.getContractEntry(),contractEntry);
            contractEntry.setUptBy(userDtl.getId());
            contractEntry.setUptTime(new Date());
        }else{
            //新增
            BeanUtils.copyProperties(vo.getContractEntry(),contractEntry);
            contractEntry.setAddBy(userDtl.getId());
            contractEntry.setAddTime(new Date());
        }
        return contractEntryService.saveAll(submitType,bodyStr,contractEntry,vo.getBpaContractListings(),userDtl);
    }
}
